package com.gosling.cloudspace.controller;

import com.alibaba.fastjson.JSON;
import com.gosling.cloudspace.common.entity.WhereSqlEntity;
import com.gosling.cloudspace.service.UtilsServiceImpl;
import com.gosling.cloudspace.tools.ImgsSelectTool;
import com.gosling.cloudspace.tools.MD5Tool;
import com.gosling.cloudspace.tools.SqlTool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工具类管理
 *
 * @email 1793327033@qq.com
 * @classname SysUtilsController
 * @author: osc_lihonglin
 * @date: 2021/7/23 9:21
 */
@RestController
@RequestMapping("/utils")
@Api(tags = "工具类接口")
public class SysUtilsController {
    @Resource
    private UtilsServiceImpl utilsService;

    /**
     * MD5进行加密
     *
     * @return
     */
    @PostMapping(value = "/md5/generate", produces = "application/json;charset=UTF-8")
    public String md5(@RequestBody String encrypt) {
        return MD5Tool.generate(encrypt);
    }

    /**
     * 随机获取一个头像
     *
     * @return
     */
    @GetMapping(value = "/random/getHeaderImgFile")
    public String getHeaderImgFile() {
        return ImgsSelectTool.getHeaderImgFile();
    }

    /**
     * SM2进行加密
     *
     * @return
     */
    @PostMapping(value = "/sm2/generate", produces = "application/json;charset=UTF-8")
    public String sm2(@RequestBody String encrypt) {
        return utilsService.sm2Generate(encrypt);
    }

    /**
     * SM2进行解密
     *
     * @return
     */
    @PostMapping(value = "/sm2/sm2Decrypt", produces = "application/json;charset=UTF-8")
    public String sm2Decrypt(@RequestBody String encrypt) {
        return utilsService.sm2Decrypt(encrypt);
    }

    /**
     * SQL批量生成
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/sql/sqlExecute")
    public void sqlExecute(@RequestParam(value = "file", required = false) MultipartFile file, String params) {
        WhereSqlEntity sqlEntity = JSON.parseObject(params, WhereSqlEntity.class);
        sqlEntity.setResourceFile(file);
        SqlTool.executor(sqlEntity);
    }

    /**
     * csv数值精度丢失转换
     *
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/csv/csv2xls")
    @ApiOperation(value = "csv数值精度丢失转换")
    public void numBerTransform(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletResponse response) {
        utilsService.csv2xlsx(file, response);
    }

    /***
     * 根据行号解密excel数据
     *
     * @param file
     * @param rows
     * @param outPath
     * @return
     */
    @PostMapping(value = "/decryptMode")
    @ApiOperation(value = "根据行号解密excel数据")
    public void decryptMode(@RequestParam("file") MultipartFile file, @RequestParam("rows") List<Integer> rows, @RequestParam("outPaht") String outPath) {
        utilsService.decryptMode(file, rows, outPath);
    }

}
